import {Module} from "vuex"
import {User} from "@/views/system/user/hooks/useTable";
import {getUserInfo} from "@/api";
const SET_USER_INFO = "SET_USER_INFO"
interface UserState{
    userInfo?: User;
}
const userModule: Module<UserState,any> ={
    state:{
        userInfo:undefined
    },
    mutations:{
        [SET_USER_INFO](state,val){
            state.userInfo = val
        }
    },
    actions:{
       async  setUserInfo({commit}){
         const  {data}  =   await getUserInfo()
           commit(SET_USER_INFO,data)
       }
    },
    getters:{
      userInfo(state){
          return state.userInfo
      },
      roles(state){
          if(!state.userInfo){
              return []
          }
          return  state.userInfo.roles.map(r=>r.roleName)
      },
      dept(state){
          return state.userInfo?.dept
      }
    }
}
export default userModule
